"""
主要测试 cnode社区的话题相关的功能
主要api包括：
1.  主题首页
"""
# 引入第三方模块
import requests
import business.common as common


def test_topic_page():
    """
    测试主题首页功能
    get请求方法
    url： http://49.233.108.117:3000/api/v1/topics
    :return:
    """
    # 1. 发送get请求
    url = 'http://49.233.108.117:3000/api/v1/topics'
    getdata = {
        "page": 1,
        "tab": "ask",
        "limit": 10,
        "mdrender": "true"
    }
    res = requests.get(url=url, params=getdata)
    print(res)
    # 2.查看状态码
    print('状态码：', res.status_code)
    # 3. 查看json结果
    print(res.json())

    jsondata = res.json()
    data = jsondata['data']
    assert len(data) == 10  # 加上断言判断len长度 =100

    for d in data:
        # print(d['tab'])
        assert d['tab'] == 'ask'  # 加上断言 进行判断
        # if d['tab'] != 'ask':
        #     print('数据验证失败')


test_topic_page()


#
def test_create_topic():
    """
    创建主题
    :return:
    """
    create_url = "http://49.233.108.117:3000/api/v1/topics"
    post_data = {"accesstoken": "8aac85ba-e4b2-4093-ac23-b4154fcca626",
                 "title": "helloworld",
                 "tab": "ask",
                 "content": "xxxxxxxxxx"}
    res1 = requests.post(url=create_url, data=post_data)
    res2 = requests.post(url=create_url, data=post_data)
    # print(res.status_code)
    res1_topic_id = res1.json()['topic_id']
    res2_topic_id = res2.json()['topic_id']
    assert not res1_topic_id == res2_topic_id

    # print(res.json())


test_create_topic()


def test_topic_detail():
    """
    主题详情
    :return:
    """
    post_data = {"accesstoken": common.get_token(),
                 "title": "helloworld",
                 "tab": "ask",
                 "content": "xxxxxxxxxx"}
    topic_id = common.create_topic(post_data)

    detail_url = "http://49.233.108.117:3000/api/v1/topic/" + topic_id
    res = requests.get(detail_url)
    # ztxq_date = {"accesstoken": "8aac85ba-e4b2-4093-ac23-b4154fcca626",
    #              "mdrender": "true"}
    # xq = requests.get(url=detail_url, data=ztxq_date)

    # print(xq.status_code)  意思是状态码=200
    assert res.status_code == 200

    # print(xq.json())
    jsondata = res.json()

    # 断言title字段  意思是从title里面的字 跟 json里面进行判断  实际结果 ==预期结果
    assert post_data['title'] == jsondata['data']['title']
    assert post_data['tab'] == jsondata['data']['tab']
    # 需要把获取到的内容  跟后面匹配
    assert post_data['content'] in jsondata['data']['content']

def test_topic_update():
    """
    编辑主题
    :return:
    """
    post_data = {"accesstoken": common.get_token(),
                 "title": "helloworld",
                 "tab": "ask",
                 "content": "xxxxxxxxxx"}

    topic_id = common.create_topic(post_data)
    print('========',topic_id)

    url = "http://49.233.108.117:3000/api/v1/topics/update"
    bjzt_date = {"accesstoken":common.get_token(),
                 "title":"火龙果大作战",
                 "tab":"ask",
                 "topic_id": topic_id,
                 "content":"嘻嘻"}


    bj = requests.post(url, data=bjzt_date)
    print(bj.json())
    #更新后的id 与原来更新之前的id 保持一致
    update_topic_id = bj.json()['topic_id']
    assert topic_id == update_topic_id


    #更新之后的内容进行断言
    #1.获取更新之后的内容
    res = common.get_topic_detail(update_topic_id)
    jsondata = res.json()
    # 断言title字段  意思是从title里面的字 跟 json里面进行判断  实际结果 ==预期结果
    assert bjzt_date['title'] == jsondata['data']['title']
    assert bjzt_date['tab'] == jsondata['data']['tab']
    assert bjzt_date["content"] in jsondata['data']['content']


def test_topic_collect():
    """
    主题收藏
    :return:
    """
    post_data = {"accesstoken": common.get_token(),
                 "title": "helloworld",
                 "tab": "ask",
                 "content": "xxxxxxxxxx"}
    topic_id = common.create_topic(post_data)
    print('========', topic_id)

    url = "http://49.233.108.117:3000/api/v1/topic_collect/collect"
    bjzt_date = {"accesstoken": common.get_token(),
                 "topic_id": topic_id}
    bj = requests.post(url, data=bjzt_date)
    print(bj.json())
    assert bj.json()['success'] == True




#     create_url = 1"http://49.233.108.117:3000/api/v1/topic_collect/collect"
#     post_data = {"accesstoken": "e983ec8e-fc0b-4be2-845c-508452c4ad9a",
#                  "topic_id": "5f04378f357c334168d772e2"
#                 }
#     res = requests.post(url=create_url, data=post_data)
#     print(res.status_code)
#     print(res.json())
# test_topic_collect()


def test_topic_de_collect():
    """
    取消收藏
    :return:
    """


#     create_url = "http://49.233.108.117:3000/api/v1/topic_collect/de_collect"
#     post_data = {"accesstoken": "e983ec8e-fc0b-4be2-845c-508452c4ad9a",
#                  "topic_id": "5f04378f357c334168d772e2"
#                  }
#     res = requests.post(url=create_url, data=post_data)
#     print(res.status_code)
#     print(res.json())
test_topic_de_collect()


def test_topic_from_user():
    """
    用户收藏的主题
    :return:
    """


#     url = 'http://49.233.108.117:3000/api/v1/topic_collect/test54'
#     res = requests.get(url=url)
#     print(res)
test_topic_from_user()

# def test_topic_reply():
#     """
#     话题评论
#     :return:
#     """
#     url = "http://49.233.108.117:3000/api/v1/topic/5f0425e303297326885143f4/replies"
#     post_data = {"accesstoken":"e983ec8e-fc0b-4be2-845c-508452c4ad9a",
#                  "content":"xiaoming","reply_id":"5f044b31357c334168d77974"
#                  }
#     res = requests.post(url=url, data=post_data)
#     print(res.status_code)
#     print(res.json())
# test_topic_reply()

# def test_reply_up():
#     """
#     评论点赞
#     :return:
#     """
#     url = "http://49.233.108.117:3000/api/v1/reply/5f043d58357c334168d7772d/ups"
#     post_data = {"accesstoken":"e983ec8e-fc0b-4be2-845c-508452c4ad9a"}
#     res = requests.post(url=url, data=post_data)
#     print(res.status_code)
#     print(res.json())
# test_reply_up()

# def test_user_info():
#     """
#     用户详情
#     :return:
#     """
#     url = "http://49.233.108.117:3000/api/v1/user/test30"
#     res = requests.get(url=url)
#     print(res.status_code)
#     print(res.json())
# test_user_info()

# def test_user_accesstoken():
#     """
#     验证token的正确性
#     :return:
#     """
#     url = "http://49.233.108.117:3000/api/v1/accesstoken"
#     post_data = {"accesstoken":"e983ec8e-fc0b-4be2-845c-508452c4ad9a"}
#     res = requests.post(url=url,data=post_data)
#     print(res.status_code)
#     print(res.json())
# test_user_accesstoken()
